#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

#include <stdio.h>
#include <norflash_drv.h>
#include "sfud.h"

/* Definitions of physical drive number for each drive */
#define DEV_NORFLASH		0	/* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status(
    BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
    return RES_OK;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive     ´洢Ʒǽ¶¯³õʼ»¯                                            */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize(
    BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
    uint8_t mid = 0;
    uint16_t did = 0;
    switch (pdrv) {
    case DEV_NORFLASH:
        NorFlashDrvInit();
        ReadNorflashID(&mid, &did);
        if (((uint32_t)mid << 16 | did) == NORFLASH_MID_DID) {
            return RES_OK;
        }
    }
    return RES_NOTRDY;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)  ´Ӵ洢Ʒ֐¶Áȡʽ¾ݠ                                     */
/*-----------------------------------------------------------------------*/

DRESULT disk_read(
    BYTE pdrv,		/* Physical drive nmuber to identify the drive */
    BYTE* buff,		/* Data buffer to store read data */
    LBA_t sector,	/* Start sector in LBA */
    UINT count		/* Number of sectors to read */
)
{

    switch (pdrv) {
    case DEV_NORFLASH:
        ReadNorflashData(sector * NORFLASH_SECTOR_SIZE, buff, count * NORFLASH_SECTOR_SIZE);
        return RES_OK;
    }

    return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)   ϲ´¢´憷֐дʽ¾ݠ                                    */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write(
    BYTE pdrv,			/* Physical drive nmuber to identify the drive */
    const BYTE* buff,	/* Data to be written */
    LBA_t sector,		/* Start sector in LBA */
    UINT count			/* Number of sectors to write */
)
{
    switch (pdrv) {
    case DEV_NORFLASH:
        WriteNorflashData(sector * NORFLASH_SECTOR_SIZE, (uint8_t*)buff, count * NORFLASH_SECTOR_SIZE);
        return RES_OK;
    }
    return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/
//
DRESULT disk_ioctl(
    BYTE pdrv,		/* Physical drive nmuber (0..) */
    BYTE cmd,		/* Control code */
    void* buff		/* Buffer to send/receive control data */
)
{

    switch (pdrv) {
    case DEV_NORFLASH:
        switch (cmd) {
        case GET_SECTOR_COUNT:
            *(DWORD*)buff = NORFLASH_SECTOR_COUNT;
            break;
        case GET_SECTOR_SIZE:
            *(WORD*)buff = NORFLASH_SECTOR_SIZE;
            break;
        }
        return RES_OK;
    }

    return RES_PARERR;
}

/*
User defined function to give a current time to fatfs module
31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31)
15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2)
*/

DWORD get_fattime(void)
{

    return	 (DWORD)0;
}
